package com.qiongqi.datadir.controller;

import com.qiongqi.datadir.common.AjaxResult;
import com.qiongqi.datadir.common.BaseController;
import com.qiongqi.datadir.common.page.TableDataInfo;
import com.qiongqi.datadir.config.DataDirConfig;
import com.qiongqi.datadir.domain.BucketData;
import com.qiongqi.datadir.domain.BucketQuota;
import com.qiongqi.datadir.domain.Resource;
import com.qiongqi.datadir.service.IBucketQuotaService;
import com.qiongqi.datadir.service.IMinioService;
import com.qiongqi.datadir.service.IResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static java.util.Objects.nonNull;
import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
import static org.apache.commons.collections4.MapUtils.getString;
import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

@Slf4j
@Controller
@RequestMapping("/res")
public class ResourceController extends BaseController {

    @Autowired
    private DataDirConfig dataDirConfig;

    @Autowired
    private IMinioService minioService;

    @Autowired
    private IResourceService resourceService;

    @Autowired
    private IBucketQuotaService bucketQuotaService;

    /**
     * 查询资源记录列表
     */
    @GetMapping
    public String index(Model model) {
        model.addAttribute("acconName", dataDirConfig.getName());
        return "index";
    }

    /**
     * 查询资源记录列表
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(Resource resource) {
        startPage();
        List<Resource> list = resourceService.selectOtherAcconResourceList(resource);
        return getDataTable(list);
    }

    /**
     * 获取资源记录详细信息
     */
    @GetMapping("/parts")
    @ResponseBody
    public List<String> parts() {
        List<String> resOwners = resourceService.selectResourceOwners();
        if (isNotEmpty(resOwners)) {
            resOwners.removeIf(owner -> equalsIgnoreCase(owner, dataDirConfig.getName()));
        }
        return resOwners;
    }

    /**
     * 获取资源记录详细信息
     */
    @GetMapping("/{id}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("id") String id) {
        log.info("id:{}", id);
        return success(resourceService.selectResourceById(id));
    }

    /**
     * 获取资源记录详细信息
     */
    @GetMapping("/choose/{id}")
    public String choose(@PathVariable("id") String id, Model model) {
        model.addAttribute("resId", id);
        return "reschoose";
    }
}
